package com.coderfans.datagithub.service;

import com.coderfans.datagithub.dao.UserDao;
import com.coderfans.datagithub.model.UserModel;
import com.coderfans.datagithub.processservice.SinglePageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${fanchunshuai} on 2016-11-17.
 *
 * @version: V1.0
 * @Desc: github数据爬取服务接口
 * @Copyright (c) 2016 58到家-版权所有
 */
@Service
public class GitHubService {
    @Autowired
    public UserDao userDao;

    /**
     * 爬取用户页面数据
     */
    public void spiderUser(String url){
        SinglePageModel singlePageModel = new SinglePageModel(new UserModel().getXpathMap());
        UserModel userModel = (UserModel) singlePageModel.runSpider(url,4,UserModel.class);
       // userDao.insertUser(userModel);
    }

    /**
     * 爬取github组织页面的链接，获取组织中的人员信息
     */
    public void spiderOrg(){
        Map<String,String> orgModelMap =  new HashMap<>();
        orgModelMap.put("hrefList","//a/@href");
        orgModelMap.put("orgUserNum","//nav[@class='orgnav']/a//span[@class='counter']/text()");

        SinglePageModel singlePageModel = new SinglePageModel(orgModelMap);
        Map<String,Object> map = null;
        try {
            String  url  = getOrgUserHref("https://github.com/apache");
            map = singlePageModel.runSpider("https://github.com/apache",4);
            //计算有多少页用户数据
            int orgUserNum = Integer.parseInt(map.get("orgUserNum").toString());
            int pageNum = 0;
            if(orgUserNum > 0){
                pageNum = orgUserNum / 30;  //默认每页用户30个
                if(orgUserNum % 30 != 0){
                    pageNum = pageNum + 1;
                }
            }

            for (int i = 0; i < pageNum ;i++){
                map = singlePageModel.runSpider(url+(i+1),4);
                List<String> stringList = (List<String>)map.get("hrefList");
                //  System.out.println("pageNum:"+(i+1)+",hrefList:"+map.get("hrefList").toString());
                System.out.println("pagenum"+(i+1)+",size = "+stringList.size());
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    /**
     * 组织url
     * @param url
     * @return https://github.com/apache ----> https://github.com/orgs/apache/people?page=1
     */
    public static String getOrgUserHref(String url ){
        System.out.println(url.split("/")[2]);
        String urlArray [] = url.split("/");
        String newUrl = urlArray[0]+"//"+urlArray[2]+"/orgs/"+urlArray[3]+"/people?page=";
        return newUrl;
    }
}
